from django.shortcuts import render
from Zhou_Kao.models import *
from rest_framework.views import APIView
from rest_framework.response import Response
from Zhou_Kao.serializers import *
from django.contrib.auth.hashers import make_password
from django.core.paginator import Paginator
from django.db.models import F

#商品添加
class ClogView(APIView):
    def post(self,request):
        try:
            ser = CLsrt(data=request.data)
            ser.is_valid()
            ser.save()
            res_data = {
                "code":200,
                "msg":"添加成功"
            }
            return Response(res_data,status=200)

        except:
            res_data = {
                "code":400,
                "msg":"添加失败"
            }
            return Response(res_data,status=200)
#分类添加
class CclView(APIView):
    def post(self,request):
        try:
            ser = CSser(data=request.data)
            ser.is_valid()
            ser.save()
            res_data = {
                "code":200,
                "msg":"添加成功"
            }
            return Response(res_data,status=200)

        except:
            res_data = {
                "code":400,
                "msg":"添加失败"
            }
            return Response(res_data,status=200)
#分页展示
class GoogShow(APIView):
    def get(self,request):
        data= request.query_params
        page = data["page"]
        alls = Googs.objects.all()
        showf = Paginator(alls,3)
        googs_info = showf.page(page)
        googs_list = [i for i in showf.page_range]
        ser_goods = CLsrt(googs_info,many=True)
        data = ser_goods.data
        res_data = {
            "code":200,
            "msg":"展示成功",
            "sf":showf.num_pages,
            "data":data,
            "gi":googs_info.number,
            "googs_list":googs_list
        }
        return Response(status=200,data=res_data)
# 游戏添加
class YxResView(APIView):
    def post(self,request):
        yxname = request.data.get("yxname","")
        try:
            yres = YxRres.objects.create(yxname=yxname)
            print(yres)
            res_data={
                "code":200,
                "msg":"添加成功"
            }
            return Response(res_data,status=200)
        except:
            res_data = {
                "code": 400,
                "msg": "添加失败"
            }
            return Response(res_data, status=200)
#外键关联
class YxUsView(APIView):
    def get(self,request):
        yxshow = YxRres.objects.all()
        strs = YxStr(yxshow,many=True)
        return Response(strs.data)
#英雄添加
class UsResView(APIView):
    def post(self,request):
        data=request.data
        print(data)
        password = data["password"]

        print(data)
        password = make_password(password)
        try:
            with open(data["img"].name,"wb") as f:
                for i in data["img"].chunks():
                    f.write(i)
                UserRest.objects.create(usernmae=data["username"],password=password,
                                       yxres_id=data["ssd"])
                res_data={
                    "code":200,
                    "msg":"成功"
                }
                return Response(res_data,status=200)
        except Exception as e:
            res_data = {
                "code": 400,
                "msg": "失败"
            }
            return Response(res_data, status=200)
#英雄展示
class YxShowView(APIView):
    def get(self,request):
        ysshow =UserRest.objects.all()
        strd = UsStr(ysshow,many=True)
        return Response(strd.data)


        # pnum = request.GET.get("pnum",1)
        # pnum = int(pnum)
        # print(pnum)
        # if pnum:
        #     strs = YxRres.objects.all()
        #     pagintor = Paginator(strs,3)
        #     page = pagintor.get_page(pnum)
        #     resp ={}
        #     resp["data"] = YxStr(page,many=True).data
        #     resp["prev"] = pnum - 1
        #     resp["next"] = pnum + 1
        #     resp["yylist"] = [num for num in range(1,pagintor.num_pages + 1)]
        #     return Response(resp)
# 游戏删除
class YxDlView(APIView):
    def get(self,request):
        data = request.query_params
        id = data["id"]
        print(id)
        try:
            YxRres.objects.filter(id=id).delete()
            res_data={
                "code":200,
                "mag":"删除成功"
            }
            return Response(res_data,status=200)
        except:
            res_data = {
                "code": 500,
                "mag": "删除失败"
            }
            return Response(res_data, status=200)
#游戏修改
class YxUpView(APIView):
    def post(self, request):
        data = request.data
        yxname = data["yxname"]
        id = data["id"]
        print(id)
        try:
            yres = YxRres.objects.filter(id=id).update(yxname=yxname)
            # yres = YxRres.objects.create(yxname=yxname)
            print(yres)
            res_data = {
                "code": 200,
                "msg": "修改成功"
            }
            return Response(res_data, status=200)
        except:
            res_data = {
                "code": 400,
                "msg": "修改失败"
            }
            return Response(res_data, status=200)
#英雄删除
class UsDlView(APIView):
    def get(self,request):
        data = request.query_params
        id = data["id"]
        print(id)
        try:
            UserRest.objects.filter(id=id).delete()
            res_data={
                "code":200,
                "mag":"删除成功"
            }
            return Response(res_data,status=200)
        except:
            res_data = {
                "code": 500,
                "mag": "删除失败"
            }
            return Response(res_data, status=200)
#英雄修改
class UsUpView(APIView):
    def post(self,request):
        data=request.data
        password = data["password"]
        usernmae = data["usernmae"]
        print(data)
        password = make_password(password)
        print(password)
        try:
            with open(data["img"].name,"wb") as f:
                for i in data["img"].chunks():
                    f.write(i)
                UserRest.objects.filter(usernmae=usernmae).update(usernmae=data["usernmae"],password=password,
                                       yxres_id=data["ssd"])
                res_data={
                    "code":200,
                    "msg":"成功"
                }
                return Response(res_data,status=200)
        except Exception as e:
            res_data = {
                "code": 400,
                "msg": "失败"
            }
            return Response(res_data, status=200)
#周测添加
class UserRegView(APIView):
    def post(self,request):
        data = request.data
        username = data["username"]
        password = data["password"]
        phone = data["phone"]
        email = data["email"]
        if not all([username,password,phone,email]):
            res_data={"code":4005,"msg":"数据不完整"}
            return Response(res_data,status=200)
        a =  r"1[\d]{10}$"
        b = r"[\s]@qq.com"
        if not a:
            res_data = {"code": 4006, "msg": "手机号不完整"}
            return Response(res_data, status=200)
        if not b:
            res_data = {"code": 4007, "msg": "邮箱不完整"}
            return Response(res_data, status=200)
        password1 = make_password(password)
        try:
            UserH.objects.create(username=username,password=password1,
                                 phone=phone,email=email)
            res_data = {"code":200, "msg": "添加成功"}
            return Response(res_data, status=200)
        except Exception as e:
            res_data = {"code":500, "msg": "添加失败"}
            return Response(res_data, status=200)
class UserShowView(APIView):
    def get(self,request):
        users=UserH.objects.all()
        print(users)
        res = UsHStr(users,many=True)
        return Response(res.data)
#省展示
class RegionView(APIView):
    def get(self,request):
        fenleishow = RegIon.objects.filter(r_id=None,)
        feileis = RegRes(fenleishow,many=True)
        data = feileis.data
        res_data={
            "code":200,
            "data":data,
            "msg":"成功"
        }
        return Response(res_data,status=200)
#下辖展示
class RegtowView(APIView):
    def get(self,request):
        r_id = request.query_params.get("r_id")
        print(r_id)
        fenleishow = RegIon.objects.filter(r_id=r_id)
        feileis = RegRes(fenleishow,many=True)
        data = feileis.data
        res_data={
            "code":200,
            "data":data,
            "msg":"成功获取"
        }
        return Response(res_data,status=200)
#作者登录
class Author(APIView):
    def get(self,request):
        name1 = request.query_params.get("name")
        print(name1)
        try:
            Author.objects.filter(name=name1).first()
            res_data={
                "code":200,
                "msg":"登录成功"
            }
            return  Response(res_data,status=200)
        except:
            res_data={
                "code":400,
                "mas":"登陆失败"
            }
            return Response(res_data,status=200)
class BookShowView(APIView):
    def get(self,request):
        id = request.query_params.get("id","")
        print(id)
        try:
            data = Book.objects.filter(author_id=id)
            s_data=BookRES(data,many=True)
            res_data = {
                "code":200,
                "mag":"获取成功",
                data :s_data
            }
            return Response(res_data,status=200)
        except:
            res_data = {
                "code": 400,
                "mag": "获取失败",
            }
            return Response(res_data, status=200)
# 测试添加
class BssView(APIView):
    def post(self,request):
        data=request.data
        print(data)
        try:
            with open(data["img"].name,"wb") as f:
                for i in data["img"].chunks():
                    f.write(i)
                Bss.objects.create(name=data["name"],price=data["price"],
                                   status=data["status"],ass_id=data["ass_id"])
                res_data={
                    "code":200,
                    "msg":"成功"
                }
                return Response(res_data,status=200)
        except Exception as e:
            res_data = {
                "code": 400,
                "msg": "失败"
            }
            return Response(res_data, status=200)
class AssView(APIView):
    def get(self,request):
        ass =Ass.objects.all()
        print(ass)
        strd = AssRES(ass,many=True)
        return Response(strd.data)

class BookView(APIView):
    def get(self,request):
        data = request.query_params
        page = data["page"]
        alls = Book.objects.all()
        fyall = Paginator(alls,3)
        book_info = fyall.page(page)
        booklist = [i for i in fyall.page_range]
        bookres = BookRES(book_info,many=True)
        data = bookres.data
        res_data={
            "code":200,
            "msg":"成功",
            "ssa":fyall.num_pages,
            "data":data,
            "gi":book_info.number,
            "fylist":booklist
        }
        return Response(res_data,status=200)

class CeshiView(APIView):
    def get(self,request):
        data = request.query_params
        try:
            Ceshi.objects.filter(name=data["name"],password=data["password"]).first()
            res_data={
                "code":200,
                "mag":"验证成功"
            }
            return Response(res_data,status=200)
        except:
            res_data = {
                "code": 200,
                "mag": "验证失败"
            }
            return Response(res_data, status=200)


class BKbooksVirw(APIView):
    def post(self,request):
        data = request.data
        print(data)
        try:
            with open(data["img"].name,"wd")as f:
                for i in data["img"].chunks():
                    f.write(i)
                    BKbook.objects.create(name=data["name"],price=data["price"],author_id=data["ssa"])
                    res_data={
                        "code":200,
                        "msg":"添加成功"
                    }
                    return Response(res_data,status=200)
        except:
            res_data = {
                "code": 200,
                "msg": "添加失败"
            }
            return Response(res_data, status=200)
    def get(self,request):
        data = request.query_params#获取前端传来的数据
        page = data["page"] #找到数据里传过来的值
        bkall = BKbook.objects.all()    #把对应数据库里的数据全部获取到
        bkfy = Paginator(bkall,1)   #把数据全部转换成分页形式的数据
        bknumber = bkfy.page(page)  #页码数据
        bklist = [i for i in bkfy.page_range]   #循环数据为一个列表
        bkres = BKbookSRE(bknumber,many=True)   #序列化页码数据
        data = bkres.data #进行赋值
        res_data={
            "code":200,
            "msg":"获取成功",
            "bklist":bklist,    #循环列表
            "ssa":bkfy.num_pages,   #对应页数数据
            "data":data     #展示的数据
        }
        return Response(res_data,status=200)

class AUthView(APIView):
    def get(self,request):
        auth = AUth.objects.all()
        authser = AssRES(auth,many=True)
        return Response(authser.data)

class TUView(APIView):
    def post(self,request):
        data= request.data
        print(data)
        try:
            # TuPian.objects.create(name=data["name"],img=data["img"])
            tures = TuPianres(data=data)
            tures.is_valid()
            tures.save()
            data={
                "code":200,
                "msg":"成功"
            }
            return Response(data,status=200)
        except:
            return Response({"code":500},status=200)
class TushowView(APIView):
    def get(self,request):
        tuall = TuPian.objects.all()
        tures = TuPianres(tuall,many=TuPian)
        data = tures.data
        data = {
            "code":200,
            "data":data
            }
        return Response(data,status=200)

class BookSView(APIView):
    def post(self,request):
        data = request.data
        try:
            booksres = BookRer1(data=data)
            booksres.is_valid()
            booksres.save()
            res_data={
                "code":200,
                "msg":"添加成功"
            }
            return Response(res_data,status=200)
        except:
            res_data = {
                "code": 500,
                "msg": "添加失败"
            }
            return Response(res_data, status=200)
class BookSshow(APIView):
    def get(self,request):
        data = request.query_params
        page = data["page"]
        print(page)
        #F __gt 用来判断两个字段的比较
        bookall = BookS.objects.filter(read__gt=F("commit"))
        fybook = Paginator(bookall,3)
        book_info = fybook.page(page)
        booklist = [i for i in fybook.page_range]
        bookres = BookRer1(book_info,many=True)
        data = bookres.data
        print(data)
        res_data={
            "code":200,
            "msg":"展示成功",
            "data":data,
            "fylist":booklist,
            "ssa":fybook.num_pages
        }

        return Response(res_data,status=200)















